package general.servlet;

import com.alibaba.fastjson.JSONObject;
import general.mapper.TestMapper;
import general.servlet.util.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @desc 描述
 * create on 2018/10/25 by watson
 */
@WebServlet(name = "ServletLoadStart", urlPatterns = "/", loadOnStartup = 1)
public class ServletLoadStart extends HttpServlet {

    public static SqlSessionFactory sqlSessionFactory;

    public void init(ServletConfig config) throws ServletException {
        System.out.println("开始初始化MyBatis");
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("/mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        System.out.println("MyBatis 初始化 sqlSessionFactory 完成");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置网页响应类型
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        //实现具体操作
        PrintWriter out = response.getWriter();
        out.println("404错误");
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void destroy() {
        System.out.println("调用销毁方法");
    }

}
